﻿using Infrastructure;
using Lan.Dto;
using Lan.Model;
using Lan.Repository;
using Lan.ServiceCore.IService;
using Model;
using SqlSugar;

namespace Lan.ServiceCore.Services
{
    /// <summary>
    /// Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IDrawPolygonService), ServiceLifetime = LifeTime.Transient)]
    public class RadarService : Repository<RadarModel>, IRadarService
    {
        public List<RadarModel> GetList(RadarQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var exp = Expressionable.Create<RadarModel>();
            exp.AndIF(!string.IsNullOrEmpty(parm.ip), u => u.Ip.Contains(parm.ip));

            var response = Queryable()
                .Where(exp.ToExpression()).ToList();

            return response;
        }

        private static Expressionable<RadarModel> QueryExp(RadarQueryDto parm)
        {
            var predicate = Expressionable.Create<RadarModel>();

            return predicate;
        }

        public RadarModel GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();
            return response;
        }

        public RadarModel AddRadar(RadarModel model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        public int UpdateRadar(RadarModel model)
        {
            return Update(model, true);
        }
        public int DeleteRadar(object id)
        {
            return Delete(id);
        }

        public List<RadarModel> GetAllList()
        {
            return Queryable()
                .Where(x => x.Status == 1)
                .ToList();
        }

        public List<RadarModel> SelectRadarIds(int BindingAreaId)
        {
            return Queryable()
                .Where(r => r.BindingAreaId == BindingAreaId)
                //.OrderBy(role => role.RoleSort)
                .ToList();
        }

        public int unUpdateBindRadar(int BindingAreaId)
        {
            var result = Update(w => w.BindingAreaId == BindingAreaId, it => new RadarModel()
            {
                BindingAreaId = -1
            });
            return result;
        }

        public int UpdateBindRadar(DefenceareaModel defence, int id)
        {
            if (defence.RadarIds == null) return 0;

            var result = 0;
            foreach (var item in defence.RadarIds)
            {
                result = Update(w => w.Id == item, it => new RadarModel()
                {
                    BindingAreaId = id
                });
            }
            return result;
        }
    }
}